package com.rxyl.love.petdoctor.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.entity.FindHospital;
import com.rxyl.love.petdoctor.entity.NearbyHospital;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/1/12 0012.
 */
public class MyListViewAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> hospitalList;

    public MyListViewAdapter(Context context, List<T> hospitalList) {
        this.context = context;
        this.hospitalList = hospitalList;
    }

    @Override
    public int getCount() {
        return hospitalList.size();
    }

    @Override
    public Object getItem(int position) {
        return hospitalList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T hospital = hospitalList.get(position);
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_layout_hospital, null);
            vh = new ViewHolder();
            vh.tv_name = (TextView) convertView.findViewById(R.id.tv_hospital_name);
            vh.tv_address = (TextView) convertView.findViewById(R.id.tv_hospital_info);
            vh.tv_distance = (TextView) convertView.findViewById(R.id.tv_hospital_distance);
            vh.tv_avgRating = (TextView) convertView.findViewById(R.id.tv_hospital_avgRating);
            vh.iv = (ImageView) convertView.findViewById(R.id.iv_hospital_head);
            vh.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar_hospital_avg);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (hospital instanceof NearbyHospital.DataEntity) {
            NearbyHospital.DataEntity entity = (NearbyHospital.DataEntity) hospital;
            vh.tv_name.setText(entity.getName());
            vh.tv_address.setText(entity.getAddress());
            String distance
                    = new DecimalFormat("0.00").format(entity.getDistance() * 1.0 / 1000) + "km";
            vh.tv_distance.setText(distance);
            vh.tv_avgRating.setText(entity.getAvg_rating() + "分");
            vh.ratingBar.setRating(entity.getAvg_rating() / 2);
            String url = entity.getPhoto_url();
            MyApplication.getApp().getImageLoader()
                    .displayImage(url, vh.iv, MyApplication.getApp().getImageOptions());

        } else if (hospital instanceof FindHospital.DataEntity.ListEntity) {
            FindHospital.DataEntity.ListEntity entity = (FindHospital.DataEntity.ListEntity) hospital;
            vh.tv_name.setText(replaceStr(entity.getName()));
            vh.tv_address.setText(entity.getAddress());
            String distance
                    = new DecimalFormat("0.00").format(entity.getDistance() * 1.0 / 1000) + "km";
            vh.tv_distance.setText(distance);
            vh.tv_avgRating.setText(entity.getAvg_rating() + "分");
            vh.ratingBar.setRating((float) entity.getAvg_rating() / 2);
            String url = entity.getPhoto_url();
            MyApplication.getApp().getImageLoader()
                    .displayImage(url, vh.iv, MyApplication.getApp().getImageOptions());
        }

        return convertView;
    }

    public String replaceStr(String name) {
        name = name.replace("<font color=\"#00a0e9\">", "");
        name = name.replace("</font>", "");
        return name;
    }

    class ViewHolder {
        ImageView iv;
        TextView tv_name, tv_distance, tv_avgRating;
        TextView tv_address;
        RatingBar ratingBar;
    }
}
